<?php
/**
 * 押金管理
*/

class Action
{
    private $per_page = 100;
    
    /**
     * 为用户添加押金
     */
    public function add_deposit_money()
    {
        $id = Request::getParam('id', 0);
        
        if (!$id) {
        	return false;
        }
        
        $user_info = _uri('user', $id);
        if (!$user_info) {
        	return '此用户不存在!';
        }
        
        $filter = array();
        $parent_type_list = _model('deposit_money_type')->getList(array('parent_id'=>0, 'status'=>1));
    
        $parent_ids      = array();
        $deposit_money_type_list = array();
        if ($parent_type_list) {
            foreach ($parent_type_list as $key => $value) {
    
                array_push($parent_ids, $value['id']);
                $deposit_money_type_list[$key]['id'] = $value['id'];
    
                $deposit_money_type_list[$key]['res_name'] = $value['res_name'];
                $deposit_money_type_list[$key]['type_list'] = _model('deposit_money_type')->getList(array('parent_id'=>$value['id'], 'status'=>1));
    
            }
        }
        
        Response::assign('user_info', $user_info);    
        Response::assign('deposit_money_type_list', $deposit_money_type_list);
        Response::display('admin/add_deposit_money.html');
    }
    
    
    /**
    * 为用户添加押金
    */
    public function add()
    {
        $filter = array();
        $parent_type_list = _model('deposit_money_type')->getList(array('parent_id'=>0, 'status'=>1));

        $parent_ids      = array();
        $deposit_money_type_list = array();
        if ($parent_type_list) {
            foreach ($parent_type_list as $key => $value) {

                array_push($parent_ids, $value['id']);
                $deposit_money_type_list[$key]['id'] = $value['id'];

                $deposit_money_type_list[$key]['res_name'] = $value['res_name'];
                $deposit_money_type_list[$key]['type_list'] = _model('deposit_money_type')->getList(array('parent_id'=>$value['id'], 'status'=>1));

            }
        }


        Response::assign('deposit_money_type_list', $deposit_money_type_list);
        Response::display('admin/add_deposit_money.html');
    }

    /**
    * 添加
    */
    public function create()
    {
        $deposit_money_info = Request::getParam('user_money', array());

        $user_id = $deposit_money_info['user_id'];
        //获取当前登陆的用户ID
        $admin_id = user_helper::get_admin_id();

        if (isset($deposit_money_info['user_money'])) {
            if (!is_numeric($deposit_money_info['user_money'])) {
                return '用户押金只能填数字';
            }
        } else {
            return '用户押金不能为空';
        }
        
        $user_money = array();
        $user_money['user_id']  = $user_id;
        $user_money['admin_id'] = $admin_id;
        $user_money['deposit_money']   = $deposit_money_info['user_money'];
        $user_money['add_time'] = date('Y-m-d H:i:s', time());
        $user_money['res_id']   = $deposit_money_info['res_id'];
        $user_money['extra']    = $deposit_money_info['extra'];

        $id = _model('deposit_money_record')->create($user_money);
        //增加用户的总押金
        $user_info = _uri('user', $user_id);
        
        if ($user_info) {
        	$new_user_money = $user_money['deposit_money'] + $user_info['deposit_money'];
        	_model('user')->update($user_id, array('deposit_money'=>$new_user_money));
        } else {
            return '未找到此用户信息!';
        }
        
        Response::assign('status', 1);
        Response::redirect('user_money/admin/deposit_money');

    }


    /**
    * 押金记录列表
    */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::Get('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        //过滤条件
        $filter = array();

        // 用户名的过滤
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $ids = user_helper::get_user_ids_by_name($search_filter['user_name']);
            $filter['user_id'] = $ids;
        }
        // 操作人的过滤
        if (isset($search_filter['admin_name']) && $search_filter['admin_name']) {
            $ids = user_helper::get_user_ids_by_name($search_filter['admin_name']);
            $filter['admin_id'] = $ids;
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        if (!$filter) $filter['user_id >'] = 0;

        $table_name = 'deposit_money_record';
        $deposit_money_list = get_data_list($table_name, $filter, ' ORDER BY `id` DESC ', $page, $this->per_page);
        if ($deposit_money_list) {

            // $user_money_list = user_money_helper::user_money_record_format($user_money_list);
            // 分配用户名和操作人姓名
            foreach ($deposit_money_list as $k => $v) {
                // 押金类型
                $type_info = _uri('deposit_money_type', $v['res_id']);
                if ($type_info && $type_info['status'] == 1) {
                    $deposit_money_list[$k]['res_name'] = $type_info['res_name'];
                } else {
                    $deposit_money_list[$k]['res_name'] = '其他途径';
                }
                $deposit_money_list[$k]['user_name'] = user_helper::display_name($v['user_id']);
            }
        }


        Response::assign('deposit_money_list', $deposit_money_list);
        Response::assign('status', $status);

        Response::assign('is_advenced', $is_advenced);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/deposit_money_record_list.html');
    }

    /**
    * 添加数据不用
    */
    /*public function add_record()
    {
        for ($i=0; $i < 100; $i++) {
            //创建数据
            $record = array();
            $record['user_id']  = mt_rand(1, 100);
            $record['user_money']    = mt_rand(10, 1000);
            $record['add_time'] = date('Y-m-d H:i:s', time());
            $record['status']   = 1;
            _model('user_money_record')->create($record);
        }

    }*/

    /**
    * 伪删除
    */
    public function delete()
    {
        $id = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('deposit_money_record')->update($v, array('status' => 0));
        }

        return 'ok';
    }

    /**
    * 恢复
    */
    public function change_status()
    {
        //接收id
        $id = Request::Get('id', 1);

        _model('deposit_money_record')->update($id, array('status' => 1));

        Response::assign('status', 1);
        Response::redirect(AnUrl('user_money/admin/deposit_money?status=1'));
    }

    /**
    * 物理删除
    */
    public function delete_record()
    {
        //接收id
        $id = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('deposit_money_record')->delete($v);
        }

        return 'ok';
    }

    /**
    * 接受ajax传递的用户名,进行查询
    */
    public function ajax_search()
    {
        $user_name = Request::getParam('user_name', '');

        if (!$user_name) {
            echo 0;
        }
        //根据接受的值查询符合条件的用户
        $user_ids = user_helper::get_user_ids_by_name($user_name);

        // $user_id_list = explode(',', $user_ids);

        $user_list = array();
        foreach ($user_ids as $k => $v) {
            array_push($user_list, user_helper::get_user_info($v));
        }
        if ($user_list) {
            echo json_encode($user_list);
        } else {
            echo 0;
        }

    }

    /**
    * 接受ajax传递的用户id,获取对应旗下的分店列表
    */
    public function ajax_search_store_list()
    {
        $user_id = Request::getParam('user_id', 1);

        if (!$user_id) {
            echo 0;
        }

        $store_list = array();

        // 根据用户id获取旗下的商铺列表
        $store_list = store_helper::get_store($user_id);

        if ($store_list) {
            echo json_encode($store_list);
        } else {
            echo 0;
        }

    }
}

?>